package dk.aau.walkietalkie.picture;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

//TODO: add logging, check arguments in functions (otherwise use constants), javadoc?

public class MyCamera {
	public static final int CAM_REQUEST = 101;
	private final int SCALE_FACTOR = 1;
	private final int QUALITY_FACTOR = 70;
	private static String IMAGEPATH;
	// public static final String IMAGE_PATH = Environment
	// .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
	// .getPath()
	// + File.separator + "tmp_image.jpg";

	public static String getImagePath() {
		return IMAGEPATH;
	}

	public void takePhoto(Activity currentActivity) {
		Date now = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-DD_HH_mm_ss");
		
		IMAGEPATH = Environment.getExternalStoragePublicDirectory(
				Environment.DIRECTORY_PICTURES).getPath()
				+ "/" + formatter.format(now) + ".jpg";

		File imgFile = new File(getImagePath());
		Uri fileUri = Uri.fromFile(imgFile);

		Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri);
		currentActivity.startActivityForResult(i, CAM_REQUEST);
	}

	public void savePhoto(String imagePath) {
		// FileInputStream fis = null;
		// FileOutputStream fos = null;
		// Bitmap newBitmap = null;
		//
		// if ("".equals(imagePath)) {
		// imagePath = getImagePath();
		// }
		// try {
		// Options opt = new BitmapFactory.Options();
		// opt.inJustDecodeBounds = false;
		// BitmapFactory.decodeFile(imagePath, opt);
		// int imgWidth = opt.outWidth;
		// int imgHeight = opt.outHeight;
		//
		// int scaleFactor = Math.round((float) imgWidth / (float) imgHeight);
		// opt.inSampleSize = scaleFactor;
		// opt.inJustDecodeBounds = false;
		// } catch (Exception e) {
		// e.printStackTrace();
		// } finally {
		// if (fis != null) {
		// try {
		// fis.close();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// }
		// }
		//
		// try {
		// fos = new FileOutputStream(imagePath);
		// } catch (Exception e) {
		// e.printStackTrace();
		// } finally {
		// try {
		// if (fos != null) {
		// fos.close();
		// }
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// }
		//
	}

}
